<template>
  <div class="home-recommend">
    <h3>热门歌手</h3>
    <div class="wrapper" ref="wrapper">
      <ul class="content" ref="content">
        <li
          v-for="(item, index) in listData"
          :key="index"
          @click="selectItem(item)"
        >
          <div class="cover">
            <van-image :src="item.singer_pic" radius="1rem" />
          </div>
          <span>{{ item.singer_name }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { betterScrollHorizontal } from "../../common/betterScroll.js";
export default {
  data() {
    return {
      listData: [],
    };
  },
  created() {
    this.getSingerListData();
  },
  methods: {
    //获取热门歌手数据
    getSingerListData() {
      this.$request("get", "/singer/list").then((res) => {
        console.log(res);
        if (res.result == 100) {
          this.listData = res.data.list;
        }
        //异步数据处理
        betterScrollHorizontal(
          this,
          this.$refs.wrapper,
          this.$refs.content,
          this.listData.length,
          10
        );
      });
    },
    selectItem(item) {
      const singermid = item.singer_mid;
      //1.调用状态管理,commit提交唤醒mutations
      // this.$store.commit("saveSinger", item);
      //2.调用状态管理，通过分发action方法
      this.$store.dispatch("saveSinger", item);
      this.$router.push(`/singer/${singermid}`);
    },
  },
};
</script>

<style scoped>
.wrapper {
  width: 100%; /**容器得宽度就是屏幕得宽度 */
  overflow: hidden;
}
ul.content > li {
  width: 10rem;
  display: inline-block;
  vertical-align: top;
}
ul.content > li * {
  margin: 0 0.1rem;
}
ul.content > li > span {
  font-size: 0.8rem;
  color: #625858;
}
</style>